package
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	
	import starling.core.Starling;
	import starling.display.Button;
	import starling.display.Image;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.extensions.PDParticleSystem;
	import starling.extensions.ParticleSystem;
	import starling.textures.Texture;
	import starling.events.EnterFrameEvent;
	import flash.desktop.NativeApplication;
	import flash.events.EventDispatcher;

	public class LevelSelect extends UIScreen
	{
		private var _level1:Button;
		private var _level2:Button;
		private var _level3:Button;
		private var _back:Button;
		private var _bunkyaijou:Image;
		private var _channel:SoundChannel;
		private var _seasonID:uint;
		public function LevelSelect(seasonID:String = null)
		{
			super();
			
			_seasonID = parseInt(seasonID)+1;
			//Assets.SoundStore.play(0,999);
			//Assets.SoundStore.();
			
			var LevelSelectpxConfig:XML = XML(new Assets.LevelSelectpx());
			var LevelSelectTexture:Texture = Texture.fromBitmap(new Assets.LevelSelect());
			var ls:PDParticleSystem =new PDParticleSystem(LevelSelectpxConfig, LevelSelectTexture);
			ls.x = 160;
			ls.y = 240;
			
			_level1 = new Button(Assets.getUIScreens().getTexture("l1"));			
			_level2 = new Button(Assets.getUIScreens().getTexture("l2"));
			_level3 = new Button(Assets.getUIScreens().getTexture("l3"));						
			_back=new Button (Assets.getUIScreens().getTexture("arrowleft"));
			_bunkyaijou=new Image (Assets.getUIScreens().getTexture("BH"));
			addChild(new Image(Assets.getUIScreens().getTexture("bgwood")));
			addChild(ls);
			addChild(_level1);
			addChild(_level2);
			addChild(_level3);
			addChild(_back);
			addChild (_bunkyaijou);
			
			Starling.juggler.add(ls);
			
			ls.start();
			//ls.start(9999);
		}
		public override function onEnter():void
		{
			_channel = Assets.SoundStore.play(0,999);
		}
		
		public override function onExit():void
		{
			_channel.stop();
		}
		protected override function onAddedToStage(event:Event):void
		{
			super.onAddedToStage(event);
			_back.y= Starling.current.stage.stageHeight * 0.85;
			_bunkyaijou.x=Starling.current.stage.stageHeight * 0.02;
			var wid:Number = Starling.current.stage.stageWidth * 0.05;
			var ht:Number = Starling.current.stage.stageHeight * 0.43;
			
			//NativeApplication.nativeApplication.addEventListener(flash.events.Event.ACTIVATE, handleActivate, false, 0, true);			
			NativeApplication.nativeApplication.addEventListener(flash.events.Event.DEACTIVATE , handleDeactivate, false, 0, true);
			
			_level1.x = wid;
			_level2.x = _level1.x + _level1.width + wid;
			_level3.x = _level2.x + _level2.width + wid;
			
			_level2.y = ht;
			_level3.y = ht;
			
			_level1.y = ht;
			
			_level1.addEventListener(starling.events.TouchEvent.TOUCH, onTouched);	
			_level2.addEventListener(starling.events.TouchEvent.TOUCH, on2Touched);
			_level3.addEventListener(starling.events.TouchEvent.TOUCH, on3Touched);
			_back.addEventListener(starling.events.TouchEvent.TOUCH, onTouched2);
		}
		
		private function handleDeactivate(event:flash.events.Event):void 
		{
			//the app is now losing focus
			//musicChannel.stop();
			_channel.stop();
		}
		
		private function on3Touched(e:TouchEvent):void
		{
			var touch:Touch = e.getTouch(stage);			
			
			if(touch.phase == TouchPhase.ENDED)
			{				
				Game.season = _seasonID;
				Game.level = 3;
				dispatchEvent(new UINavigateEvent(UINavigateEvent.UI_GOTO, true, {screenID:"Game"}));
			}			
		}
		
		private function on2Touched(e:TouchEvent):void
		{
			var touch:Touch = e.getTouch(stage);			
			
			if(touch.phase == TouchPhase.ENDED)
			{				
				Game.season = _seasonID;
				Game.level = 2;
				dispatchEvent(new UINavigateEvent(UINavigateEvent.UI_GOTO, true, {screenID:"Game"}));
			}			
		}
		private function onTouched(e:TouchEvent):void
		{						
			var touch:Touch = e.getTouch(stage);			
			
			if(touch.phase == TouchPhase.ENDED)
			{				
				Game.season = _seasonID;
				Game.level = 1;
				dispatchEvent(new UINavigateEvent(UINavigateEvent.UI_GOTO, true, {screenID:"Game"}));
			}
		}
		
		private function onTouched2(e:TouchEvent):void
		{						
			var touch:Touch = e.getTouch(stage);			
			if(touch != null)
			{
				if(touch.phase == TouchPhase.ENDED)
				{				
					dispatchEvent(new UINavigateEvent(UINavigateEvent.UI_BACK, true));
				}				
			}
			
		}
	/*	public override function onEnter():void
		{
			_channel = Assets.SoundStore.play(0,999);
		}
		
		public override function onExit():void
		{
			_channel.stop();
		}*/	
	}
	
}